package com.example.administrator.liveapp.activity;

import android.Manifest;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.provider.Settings;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.PermissionChecker;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

import com.example.administrator.liveapp.ApplicationUtils;
import com.example.administrator.liveapp.MainActivity;
import com.example.administrator.liveapp.R;
import com.example.administrator.liveapp.utils.EasyPermissions;
import com.example.administrator.liveapp.utils.SharedPrefsUtil;

import java.util.List;

/**
 * 过渡页activity
 */

public class SplashActivity extends AppCompatActivity implements EasyPermissions.PermissionCallback {
    private boolean isLogin;
    private SharedPrefsUtil sp;
    private String[] perms = {Manifest.permission.READ_EXTERNAL_STORAGE
            , Manifest.permission.CAMERA, Manifest.permission.READ_PHONE_STATE};
    private static final int PERMS_REQUEST_CODE = 100;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_splash);
        ApplicationUtils applicationUtils = new ApplicationUtils();
        applicationUtils.addActivity(this);
        if (!EasyPermissions.hasPermissions(SplashActivity.this, perms)) {
            EasyPermissions.requestPermissions(SplashActivity.this, "需要权限", PERMS_REQUEST_CODE, perms);
        } else {
            sp = new SharedPrefsUtil();
            isLogin = sp.getBooleanValue(SplashActivity.this, "isLogin", false);
            if (isLogin) {
                Intent intent = new Intent(this, TransitionPage.class);
                startActivity(intent);
                finish();
            } else {
                sp.putBooleanValue(SplashActivity.this, "isLogin", true);
                Intent intent = new Intent(this, WelcomePage.class);
                startActivity(intent);
                finish();
            }
        }

    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        EasyPermissions.onRequestPermissionsResult(PERMS_REQUEST_CODE, permissions, grantResults, SplashActivity.this);
    }

    /**
     * 同意
     *
     * @param requestCode
     * @param perms
     */
    @Override
    public void onPermissionsGranted(int requestCode, List<String> perms) {
        sp = new SharedPrefsUtil();
        isLogin = sp.getBooleanValue(SplashActivity.this, "isLogin", false);
        if (isLogin) {
            Intent intent = new Intent(this, TransitionPage.class);
            startActivity(intent);
            finish();
        } else {
            sp.putBooleanValue(SplashActivity.this, "isLogin", true);
            Intent intent = new Intent(this, WelcomePage.class);
            startActivity(intent);
            finish();
        }
    }

    /**
     * 拒绝
     *
     * @param requestCode
     * @param perms
     */
    @Override
    public void onPermissionsDenied(int requestCode, List<String> permss) {
        ActivityCompat.requestPermissions(this, perms, PERMS_REQUEST_CODE);
//        finish();
    }

}
